package com.cjd.common.preferences

import android.content.Context
import android.content.SharedPreferences

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2015/9/6 17 29.
 * description sharePreferences 用户登录信息 存储 不需要 Context
 */
object UserPreference {

    private val PREFERENCE_NAME = "user_info"
    private var mSharedPreferences: SharedPreferences? = null

    private var editor: SharedPreferences.Editor? = null
    private val ACCESS_TOKEN = "access_token"
    private val IS_SIGN_IN = "is_sign_in"

    @Synchronized
    fun init(context: Context) {
        mSharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
        editor = mSharedPreferences?.edit()
    }

    fun setAccessToken(accessToken: String) {
        editor!!.putString(ACCESS_TOKEN, accessToken)
        editor!!.commit()
    }

    fun getAccessToken(): String {
        return mSharedPreferences?.getString(ACCESS_TOKEN, "") ?: ""
    }

    fun signIn() {
        editor?.putBoolean(IS_SIGN_IN, true)
        editor?.commit()
    }

    fun logout() {
        editor?.putBoolean(IS_SIGN_IN, false)
        editor?.putString(ACCESS_TOKEN, "")
        editor?.commit()
    }

    fun isSignIn(): Boolean {
        return mSharedPreferences?.getBoolean(IS_SIGN_IN, false) ?: false
    }

    fun setString(key: String, value: String) {
        editor?.putString(key, value)
        editor?.commit()
    }

    fun getString(key: String): String? {
        return mSharedPreferences?.getString(key, null)
    }

    fun setBoolean(key: String, value: Boolean) {
        editor?.putBoolean(key, value)
        editor?.commit()
    }

    fun getBoolean(key: String): Boolean? {
        return mSharedPreferences?.getBoolean(key, false)
    }

    fun getInt(key: String): Int? {
        return mSharedPreferences?.getInt(key, 0)
    }

    fun setInt(key: String, value: Int) {
        editor?.putInt(key, value)
        editor?.commit()
    }

    fun getLong(key: String): Long? {
        return mSharedPreferences?.getLong(key, 0)
    }

    fun setLong(key: String, value: Long) {
        editor?.putLong(key, value)
        editor?.commit()
    }

}